package com.hourses.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hourses.mapper.ProductTypeMapper;
import com.hourses.pojo.ProductType;
import com.hourses.pojo.Result;
import com.hourses.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/6 16:25
 */
@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    //查询所有商品的商品分类树
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public List<ProductType> selectAllTypeTree() {
        //先看看缓存中有没有
        String typeTree = redisTemplate.opsForValue().get("productTypeTree");
        if (StringUtils.hasText(typeTree)){
            //有缓存
            return JSON.parseArray(typeTree,ProductType.class);
        }
        //没有缓存
        //step1:查询出所有的商品分类
        List<ProductType> productTypes = productTypeMapper.selectAllType();
        //step2:将商品分类划分成商品分类树
        List<ProductType> productTypeTree = allTypeToTree(productTypes, 0);
        //step3:存入缓存
        redisTemplate.opsForValue().set("productTypeTree",JSON.toJSONString(productTypeTree));
        return productTypeTree;
    }

    //根据type_code查询商品分类
    @Override
    public Result selectByTypeCode(ProductType productType) {
        //根据typeCode去数据库中查有没有这个商品类型
        ProductType productType1 = productTypeMapper.selectByTypeCode(productType);
        //有的话给前端返回false，没有的话给前端返回true
        return Result.ok(productType1==null);
    }

    //根据tupeName查询productType
    @Override
    public ProductType selectByTypeName(String typeName) {
        ProductType productType = productTypeMapper.selectByTypeName(typeName);
        return productType;
    }

    //增加商品分类
//    @CacheEvict("productTypeTree")    //清一下缓存，避免数据不一致
    @Override
    public Result insertProductType(ProductType productType) {
        Integer count = productTypeMapper.insertProductType(productType);
        if (count>0){
            redisTemplate.delete("productTypeTree");
            return Result.ok("添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！请联系管理员...");
    }

    //删除商品分类
    @Override
    public Result deleteProducttype(Integer typeId) {
        Integer count = productTypeMapper.deleteProducttype(typeId);
        if (count>0){
            redisTemplate.delete("productTypeTree");
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！请联系管理员...");
    }

    //修改商品分类
    @Override
    public Result updateProductType(ProductType productType) {
        //step1:先判断修改后的typeName是否已经存，如果存在，不允许修改
        ProductType productType1 = productTypeMapper.selectByTypeName(productType.getTypeName());
        if (productType1!=null&&!productType1.getTypeId().equals(productType.getTypeId())){
            //到这里了就证明修改后的typeName已经存在，不允许修改
            return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！分类名称已经存在...");
        }
        //到这里了就证明修改后的typeName不存在，允许修改
        Integer count = productTypeMapper.updateProductType(productType);
        if (count>0){
            //修改成功，清一下缓存
            redisTemplate.delete("productTypeTree");
            return Result.ok("修改成功!");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败!请联系管理员...");
    }

    //将所有商品类型转换为类型树的方法
    private List<ProductType> allTypeToTree(List<ProductType> productTypes,Integer pid){
        List<ProductType> firstLevelTree = new ArrayList<>();
        for (ProductType productType:productTypes){
            if (productType.getParentId().equals(pid)){
                firstLevelTree.add(productType);
            }
        }
        for (ProductType productType:firstLevelTree){
            List<ProductType> secondLevelTree = allTypeToTree(productTypes,productType.getTypeId());
            productType.setChildProductCategory(secondLevelTree);
        }
        return firstLevelTree;
    }
}
